//
//  SelectHouseTypeView.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/10/8.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "SelectHouseTypeView.h"

@interface SelectHouseTypeView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *contentDateView;

@property (nonatomic, strong) UIPickerView *datePick;

@property (nonatomic, strong) NSMutableArray *dateArray;

@property (nonatomic, strong) UIButton *okButton;

@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIView *topLine;

@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UILabel *titleLabel;

// date

@property (nonatomic, strong) NSString *dateString;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic, strong) NSMutableArray *returnArray;

@end

@implementation SelectHouseTypeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];
    }
    return self;
}

- (void)setupView{
    [self setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self setBackgroundColor:[UIColor clearColor]];
    
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEvent)];
    
    self.bgView = [[UIView alloc]init];
    [self.bgView setAlpha:0.5];
    [self.bgView setBackgroundColor:[UIColor blackColor]];
    [self.bgView addGestureRecognizer:gest];
    [self addSubview:self.bgView];
    
    self.contentDateView = [[UIView alloc]init];
    [self.contentDateView setBackgroundColor:HexColorInt32_t(d9d9d9)];
    [self addSubview:self.contentDateView];
    
    self.datePick = [[UIPickerView alloc]init];
    [self.datePick setBackgroundColor:[UIColor whiteColor]];
    [self.datePick setDelegate:self];
    [self.datePick setDataSource:self];
    [self.contentDateView addSubview:self.datePick];
    
    self.bottomLine = [[UIView alloc]init];
    [self.bottomLine setBackgroundColor:HexColorInt32_t(e5e5e5)];
    [self.contentDateView addSubview:self.bottomLine];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.titleLabel setTextColor:HexColorInt32_t(4d4d4d)];
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self.contentDateView addSubview:self.titleLabel];
    
    self.cancelButton = [[UIButton alloc]init];
    [self.cancelButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:HexColorInt32_t(e84515) forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentDateView addSubview:self.cancelButton];
    
    self.okButton = [[UIButton alloc]init];
    [self.okButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.okButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.okButton setTitleColor:HexColorInt32_t(e84515) forState:UIControlStateNormal];
    [self.okButton addTarget:self action:@selector(okEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentDateView addSubview:self.okButton];
    
    self.topLine = [[UIView alloc]init];
    [self.topLine setBackgroundColor:HexColorInt32_t(e5e5e5)];
    [self.contentDateView addSubview:self.topLine];
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.contentDateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(244));
    }];
    
    [self.datePick mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(0));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(200));
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(1));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(44));
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.left.equalTo(@(0));
        make.width.equalTo(@(45));
        make.height.equalTo(@(44));
    }];
    
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-200));
        make.right.equalTo(@(0));
        make.width.equalTo(@(45));
        make.height.equalTo(@(44));
    }];
    
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-239));
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.height.equalTo(@(1));
    }];
}

#pragma mark ====== UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (self.count == 1) {
        return self.dateArray.count;
    }else{
        NSArray *counArr = [self.dateArray objectAtIndex:component];
        return counArr.count;
    }
    
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (self.count == 1) {
        return [self.dateArray objectAtIndex:row];
    }else{
        NSArray *counArr = [self.dateArray objectAtIndex:component];
        return [counArr objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.count == 1) {
        self.dateString = [self.dateArray objectAtIndex:row];
    }else{
        NSArray *counArr = [self.dateArray objectAtIndex:component];
        NSString *selString = [counArr objectAtIndex:row];
        [self.returnArray replaceObjectAtIndex:component withObject:selString];
    }
    
    
}

- (void)showSelectTitle:(NSString *)title dataArray:(NSMutableArray *)data count:(NSInteger)count{
    self.count = count;
    if (count > 1) {
        for (int d = 0; d < count; d++) {
            NSArray *itearr = [data objectAtIndex:d];
            [self.returnArray addObject:[itearr firstObject]];
        }
    }
    self.dateArray = data;
    [self.datePick reloadAllComponents];
    [self.titleLabel setText:title];
    [KWindow addSubview:self];
}

- (void)cancelEvent{
    [self removeFromSuperview];
}

- (void)okEvent:(UIButton *)sender{
    if (self.dateString == nil) {
        if (self.count == 1) {
            self.dateString = [self.dateArray objectAtIndex:0];
        }else{
            self.dateString = [self manyRow];
        }
    }
    self.typePickSelect(self.dateString);
    [self removeFromSuperview];
}

- (void)cancelEvent:(UIButton *)sender{
    [self removeFromSuperview];
}

- (NSMutableArray *)dateArray{
    if (!_dateArray) {
        _dateArray = [[NSMutableArray alloc]init];
    }
    return _dateArray;
}

- (NSMutableArray *)returnArray{
    if (!_returnArray) {
        _returnArray = [[NSMutableArray alloc]init];
    }
    return _returnArray;
}

- (NSString *)manyRow{
    NSString *string = @"";
    for (NSString *item in self.returnArray) {
       string = [string stringByAppendingString:item];
    }
    return string;
}

@end
